package com.qfedu.yanglao20admin.sysmessage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.entity.sysmessage.MsgManage;
import com.qfedu.yanglao20.entity.sysmessage.MsgType;
import com.qfedu.yanglao20admin.sysmessage.param.MsgCenter;
import com.qfedu.yanglao20admin.sysmessage.service.MsgCenterServise;
import com.qfedu.yanglao20admin.sysmessage.service.MsgManageService;
import com.qfedu.yanglao20admin.sysmessage.service.MsgTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User:  yunk
 * Date 2023/4/19  21:36
 */

@Api(tags = "系统消息")
@RestController
@RequestMapping("sysmsg")
public class SysMessageController {

    @Resource
    private MsgManageService msgManageService;
    @Resource
    private MsgTypeService msgTypeService;
    @Resource
    private MsgCenterServise msgCenterServise;

    @ApiOperation(value = "启用的消息类行")
    @GetMapping("type")
    public List<MsgType> selectType() {
        return msgTypeService.slelectByType();
    }

    @ApiOperation(value = "消息管理分页查询")
    @GetMapping("manage/page")
    public CommonResult<Page<MsgManage>> queryByPage(MsgCenter msgCenter) {
        if (msgCenter.getCurrent() - 1 < 0) {
            return CommonResult.error("页码不合法");
        }
//        List<String> strings = msgTypeService.slelectByType();
        return CommonResult.success(msgManageService.queryByPage(msgCenter));
    }

    @ApiOperation(value = "消息管理添加")
    @PostMapping("insert")
    public CommonResult insertMsgManage(MsgManage msgManage) {
//        if (msgManage.getMsgTitle() == null || msgManage.getMsgTitle().isEmpty()) {
//            return CommonResult.error("主要内容不能为空");
//        }
        return CommonResult.success(msgManageService.insert(msgManage));
    }

    @ApiOperation(value = "消息管理查看")
    @GetMapping("select")
    public CommonResult queryMsgManageById(Integer id) {
        return CommonResult.success(this.msgManageService.queryById(id));
    }

    @ApiOperation(value = "根据id删除消息类型")
    @DeleteMapping("delete")
    public CommonResult deleteById(Integer id) {
        return CommonResult.success(this.msgTypeService.deleteById(id));
    }

    @ApiOperation(value = "消息类型分页查询")
    @GetMapping("type/page")
    public CommonResult selectPage(MsgType msgType) {
        if (msgType.getPageNum() - 1 < 0) {
            return CommonResult.error("页码不合法");
        }
        return CommonResult.success(this.msgTypeService.queryByPage(msgType));
    }

    @ApiOperation(value = "消息类型详情")
    @GetMapping("type/select")
    public CommonResult queryTypeById(Integer id) {
        return CommonResult.success(this.msgTypeService.queryById(id));
    }

    @ApiOperation("消息类型编辑")
    @PutMapping("update")
    public void uodateType(MsgType msgType) {
        msgTypeService.update(msgType);
    }


    @ApiOperation("消息中心分页查询")
    @GetMapping("msgcenter/page")
    public CommonResult msgcenterPage(MsgCenter msgCenter) {
        return CommonResult.success(msgCenterServise.selectPage(msgCenter));
    }

    @ApiOperation("消息中心全部已读")
    @GetMapping("msgcenter/state")
    public CommonResult msgcenterState() {
        msgCenterServise.update();
        return CommonResult.success();
    }

    @ApiOperation("消息中心查看")
    @GetMapping("msgcenter/select")
    public CommonResult msgcenterSelect(Integer id) {
        return CommonResult.success(msgCenterServise.selectById(id));
    }

    @ApiOperation("消息类型添加")
    @PostMapping("msgType/inster")
    public CommonResult insterManage(MsgType msgType) {
        return CommonResult.success(msgTypeService.insert(msgType));

    }

}
